package hz.mobileproject.pda.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.db.DBManager;
import hz.mobileproject.pda.entity.request.Request_Edu_inter;
import hz.mobileproject.pda.service.UploadFailService;
import hz.mobileproject.pda.ui.adapter.UploadFailListAdapter;

/**
 * Created by hasee on 2017/10/17.
 */

public class UploadFailActivity extends AppCompatActivity {

    private UploadFailBroadcast mReceiver = new UploadFailBroadcast();
    private List<Request_Edu_inter> mRequestInterList = new ArrayList<>();
    private UploadFailListAdapter mAdapter;
    private DBManager mDBManager;
    private ListView mListView;
    private Button mBtStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_fail);
        initView();
        initData();
        initThing();
        registerUploadFailReceiver();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

    private void initThing() {
        mBtStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mRequestInterList.size()>0){
                    try{
                        UploadFailActivity.this.startService(new Intent(UploadFailActivity.this, UploadFailService.class));
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }else{
                    ToastUtils.showShort("没有可以上传的谈话");
                }
            }
        });
    }


    private void initData() {
        mDBManager = DBManager.getInstance(getApplicationContext());
        mAdapter = new UploadFailListAdapter(this, mRequestInterList);
        mListView.setAdapter(mAdapter);
        refreshData();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.lv_upload_fail);
        mBtStart = (Button) findViewById(R.id.bt_upload_fail_start);
    }

    private void refreshData(){
        List<Request_Edu_inter> interList = mDBManager.queryRequest_Edu_inter();
        mRequestInterList.clear();
        mRequestInterList.addAll(interList);
        mAdapter.notifyDataSetChanged();
        for(int i=0;i<mRequestInterList.size();i++){
            if("2".equals(mRequestInterList.get(i).getUp_down())){
                mBtStart.setVisibility(View.GONE);
                break;
            }
        }
    }

    private void registerUploadFailReceiver() {
        try{
            IntentFilter filter = new IntentFilter();
            filter.addAction("uploadSuccess");
            filter.addAction("uploadFail");
            filter.addAction("uploadStart");
            filter.addAction("uploadFinish");
            registerReceiver(mReceiver, filter);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    class UploadFailBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case "uploadSuccess" :
                case "uploadFail" :
                    refreshData();
                    break;
                case "uploadStart" :
                    refreshData();
                    mBtStart.setVisibility(View.GONE);
                    break;
                case "uploadFinish" :
                    refreshData();
                    mBtStart.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        }
    }

}
